package com.example.demo.config.recode;

import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class ReCodeManage {
    private HashMap<String,ReCode> reCodeHashMap=new HashMap <String,ReCode>(  );


    public ReCode getCode(String code){
        if(!reCodeHashMap.containsKey( code )){
            throw new UsernameNotFoundException( "二维码参数错误！" );
        }
        return reCodeHashMap.get(  code);
    }
    public void saveCode(String code){
        ReCode reCode = new ReCode( code );
        reCodeHashMap.put( code,reCode );
    }

    public void isread(String code) {
        ReCode code1 = getCode( code );
        code1.setRead( true );
        reCodeHashMap.put( code,code1 );
    }

    public void setCode(String state, WxMpOAuth2AccessToken authUrl) {
        ReCode code = getCode( state );
        code.setInfo(authUrl  );
        reCodeHashMap.put( state,code );
    }

    public void setUser(String state, WxMpUser mpUser) {
        ReCode code = getCode( state );
        code.setMpUser(mpUser);
        reCodeHashMap.put( state,code );
    }
}
